/*
 * DCE400服务系统 -- 后端服务接口 -- 数据访问层 -- 工单数据访问
 * 包括：
 */
package dao

import (
	"errors"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/db"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/util"
	"go.uber.org/zap"
	"time"
)

//********************************************
//Func  ：查询数据库：工单列表分页查询。
//Method：
//Usage ：
//********************************************
func QueryFlowList(page, pageSize int, orderId int64) (int64, []*entity.WorkOrderFlow, error) {
	if page < 1 {
		return 0, nil, errors.New("page<1, error")
	}
	offset := (page - 1) * pageSize
	session := db.Session().Table("work_order_flow") //工单
	defer session.Close()
	if pageSize > 0 {
		session.Limit(pageSize, offset)
	}

	flows := []*entity.WorkOrderFlow{}
	total, err := session.Where("work_order_id=?", orderId).FindAndCount(&flows)
	if err != nil {
		return 0, nil, err
	}
	return total, flows, nil
}

//********************************************
//Func  ：查询数据库：指定流程的详情
//Method：GET
//Usage ：
//********************************************
func QueryFlowDetail(Id int64) (*entity.WorkOrderFlow, error) {
	session := db.Session().Table("work_order_flow")
	defer session.Close()

	var flow entity.WorkOrderFlow
	has, err := session.Where("id=?", Id).Get(&flow)
	if err != nil || has == false {
		return nil, err
	}
	return &flow, nil
}

//********************************************
//Func  ：添加新工单
//Method：POST
//Usage ：
//********************************************
func InsertFlow(flow entity.WorkOrderFlow) (*entity.WorkOrderFlow, error) {
	var err error

	flow.StartAt = time.Now()
	flow.FlowId = util.GenSnowflakeInt64()
	global.Log.Info("InsertFlow: ", zap.Any("flow:", flow))

	session := db.Session().Table("work_order_flow")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return nil, errors.New("db session begin failed.")
	}

	_, err = session.Insert(&flow) //插入
	if err != nil {
		session.Rollback()
		return nil, err
	}
	err = session.Commit()
	if err != nil {
		return nil, err
	}
	return &flow, nil
}

//********************************************
//Func  ：添加新工单
//Method：POST
//Usage ：更新以下字段：prev_flow_id,work_order_status,next_flow_id,end_at,elapsed_time,operator_id
//********************************************
func UpdateFlow(flow entity.WorkOrderFlow, cols string) (*entity.WorkOrderFlow, error) {
	var err error
	global.Log.Info("UpdateFlow: ", zap.Any("WorkOrderFlow:", flow))

	session := db.Session().Table("work_order_flow")
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return nil, errors.New("db session begin failed.")
	}
	if len(cols) <= 0 {
		cols = "prev_flow_id,work_order_status,next_flow_id,end_at,elapsed_time,operator_id"
	}
	_, err = session.Where("flow_id=?", flow.FlowId).Cols(cols).Update(&flow)
	if err != nil {
		session.Rollback()
		return nil, err
	}
	err = session.Commit()
	if err != nil {
		return nil, err
	}

	return &flow, nil
}

//********************************************
//Func  ：Customer删除
//Method：DELETE
//Usage ：客户-账号删除--物理删
//********************************************
func DeleteFlow(flowId int64) error {

	session := db.Session().Table("work_order_flow")
	defer session.Close()

	if flowId <= 0 {
		return errors.New("unknown flow id")
	}

	session.Begin()
	cnt, err := session.Where("flow_id=?", flowId).Delete(&entity.WorkOrderFlow{})
	if err != nil {
		session.Rollback()
		return err
	}

	if cnt <= 0 {
		session.Rollback()
		return errors.New("移除步骤数量0")
	}
	err = session.Commit()
	return err
}
